package com.wangjie.doublepointer;

/**
 * @author jieshao
 * @date 2022/4/29 14:57
 */
public class Test10 {
    public static void main(String[] args) {
        ListNode t5 = new ListNode(5, null);
        ListNode t4 = new ListNode(4, t5);
        ListNode t3 = new ListNode(3, t4);
        ListNode t2 = new ListNode(2, t3);
        ListNode t1 = new ListNode(1, t2);
        System.out.println("反转前：");
        for (ListNode p = t1; p != null; p = p.next) {
            System.out.println(p.val);
        }

        ListNode head = reverse(t1);

        System.out.println("反转后：");
        for (ListNode p = head; p != null; p = p.next) {
            System.out.println(p.val);
        }
    }

    public static ListNode reverse(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode last = reverse(head.next);
        head.next.next = head;
        head.next = null;
        return last;
    }
}
